<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Trying out the WebSocket API 02-06</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
        <script>
            var ws = new WebSocket('ws://echo.websocket.org');

            ws.onopen = function() {
                console.log('connected');
                console.log('> Hello');
                ws.send('Hello');
            };

            ws.onmessage = function(e) {
                console.log('< ' + e.data);
                myeventHandler('Connection is still opening..');
            };

            ws.onclose = function(e) {
                console.log('Connection closed', e);
            };

            ws.onerror = function() {
                console.log('error');
            };

            /**
             * Comment
             */
            function myeventHandler(data) {
                if (ws.readyState === WebSocket.OPEN) {
                    //The socket is open, so it is ok to send the data.
                    ws.send(data);
                } else {
                    //Do something else in this case
                    //Possible ignore the data or queue it.
                }
            };
        </script>
    </body>
</html>
